﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OGS.Contracts;

namespace OGS.DataAccess
{
    /// <summary>
    /// Represents a class used for database related work of Product
    /// </summary>
    public class DAProduct
    {

        private static readonly DAProduct instance;

        /// <summary>
        /// Initializes a static instance of the Product class with no arguments.
        /// </summary>
        static DAProduct()
        {
            instance = new DAProduct();
        }

        /// <summary>
        /// Initializes a new instance of the Product class with no arguments.
        /// </summary>
        private DAProduct() { }

        /// <summary>
        /// The Instance of Product class
        /// </summary>
        public static DAProduct Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Saves the Product
        /// </summary>
        /// <param name="objProductInfo"> The ProductInfo Object </param>
        /// <returns>Success/Faliure</returns>
        public int SaveProduct(Product objProductInfo)
        {
            DataTable dtProductLanguage = new DataTable();
            dtProductLanguage.Columns.Add("LanguageID");
            dtProductLanguage.Columns.Add("Name");
            dtProductLanguage.Columns.Add("Description");

            foreach (ProductLanguage ProductLanguage in objProductInfo.ProductLanguages)
            {
                DataRow dr = dtProductLanguage.NewRow();
                dr["LanguageID"] = ProductLanguage.LanguageID;
                dr["Name"] = ProductLanguage.Name;
                dr["Description"] = ProductLanguage.Description;
                dtProductLanguage.Rows.Add(dr);
            }

            using (DBManager dbm = new DBManager("stp_Save_Product"))
            {

                dbm.Add("@ProductID", objProductInfo.ID, ParameterDirection.InputOutput);
                if (objProductInfo.CategoryID > 0)
                {
                    dbm.Add("@CategoryID", objProductInfo.CategoryID);
                }
                dbm.Add("@URL", objProductInfo.URL);
                if (objProductInfo.BrandID > 0)
                {
                    dbm.Add("@BrandID", objProductInfo.BrandID);
                }
                dbm.Add("@SKU", objProductInfo.SKU);
                dbm.Add("@Quantity", objProductInfo.Quantity);
                dbm.Add("@UOMID", objProductInfo.UOMID);
                dbm.Add("@MRP", objProductInfo.MRP);
                dbm.Add("@Price", objProductInfo.Price);
                if (!string.IsNullOrEmpty(objProductInfo.Image_Big))
                {
                    dbm.Add("@Image_Big", objProductInfo.Image_Big);
                }
                if (!string.IsNullOrEmpty(objProductInfo.Image))
                {
                    dbm.Add("@Image", objProductInfo.Image);
                }
                dbm.Add("@IsActive", objProductInfo.IsActive);
                dbm.Add("@LastModifiedBy", objProductInfo.LastModifiedBy);

                dbm.Add("@IsPackagedItem", objProductInfo.IsPackagedItem);
                dbm.Add("@ProductLanguage", dtProductLanguage);

                dbm.ExecuteNonQuery();

                objProductInfo.ID = (int)dbm.GetValueInInt("@ProductID");
                return Convert.ToInt32(dbm.ReturnValue);


            }
        }

        /// <summary>
        /// Get Product details
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Product GetProductDetails(int ID)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_ProductDetails"))
            {
                dbm.Add("@ProductID", ID);

                dt = dbm.GetDataTable();
            }
            Product Product = new Product();

            if (dt.Rows.Count > 0)
            {
                Product = PopulateProduct(dt);
            }

            return Product;
        }

        /// <summary>
        /// Search Product
        /// </summary>
        /// <param name="objProductInfo"></param>
        /// <param name="objSearch"></param>
        /// <returns></returns>
        public List<Product> SearchProduct(Product objProductInfo, SearchAttributes objSearch)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Search_Product", objSearch))
            {
                if (objProductInfo.CategoryID > 0)
                {
                    dbm.Add("@CategoryID", objProductInfo.CategoryID);
                }
                if (objProductInfo.BrandID > 0)
                {
                    dbm.Add("@BrandID", objProductInfo.BrandID);
                }
                if (objProductInfo.ProductLanguages[0].Name != string.Empty)
                {
                    dbm.Add("@Name", objProductInfo.ProductLanguages[0].Name);
                }
                //dbm.Add("@IsActive", objBrandInfo.IsActive);
                dt = dbm.GetDataTable();
                objSearch.TotalRecordCount = Convert.ToInt32(dbm.GetValueInInt("@TotalRecordCount"));
            }

            List<Product> lstProduct = new List<Product>();
            for (int count = 0; count < dt.Rows.Count; )
            {

                Product objProduct = PopulateProduct(dt.Rows[count]);
                objProduct.BrandName = dt.Rows[count]["BrandName"].ToString();
                objProduct.CategoryName = dt.Rows[count]["CategoryName"].ToString();
                objProduct.UOMName = dt.Rows[count]["UOMName"].ToString();

                objProduct.ProductLanguages = new List<ProductLanguage>();
                objProduct.ProductLanguages.Add(new ProductLanguage
                {
                    ID = Convert.ToInt32(dt.Rows[count]["PK_ProductID"]),
                    Name = Convert.ToString(dt.Rows[count]["Name"]),
                    LanguageID = Convert.ToInt32(dt.Rows[count]["LanguageID"])
                });
                objProduct.ProductLanguages.Add(new ProductLanguage
                {
                    ID = Convert.ToInt32(dt.Rows[count + 1]["PK_ProductID"]),
                    Name = Convert.ToString(dt.Rows[count + 1]["Name"]),
                    LanguageID = Convert.ToInt32(dt.Rows[count + 1]["LanguageID"])
                });
                objProduct.ProductLanguages.Add(new ProductLanguage
                {
                    ID = Convert.ToInt32(dt.Rows[count + 2]["PK_ProductID"]),
                    Name = Convert.ToString(dt.Rows[count + 2]["Name"]),
                    LanguageID = Convert.ToInt32(dt.Rows[count + 2]["LanguageID"])
                });

                lstProduct.Add(objProduct);
                count = count + 3;

            }
            return lstProduct;
        }

        /// <summary>
        /// Populate the Product
        /// </summary>
        /// <param name="dt"> The ProductInfo DataTable </param>
        /// <returns>The Product Object </returns>
        private Product PopulateProduct(DataTable dt)
        {
            Product objProductInfo = new Product();
            DataRow dr = dt.Rows[0];
            objProductInfo = PopulateProduct(dr);

            foreach (DataRow drLang in dt.Rows)
            {
                objProductInfo.ProductLanguages.Add(new ProductLanguage
                {
                    ID = Convert.ToInt32(drLang["PK_ProductID"]),
                    LanguageID = Convert.ToInt32(drLang["LanguageID"]),
                    Name = Convert.ToString(drLang["Name"]),
                    Description = Convert.ToString(drLang["Description"])
                });
            }


            return objProductInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private Product PopulateProduct(DataRow dr)
        {
            Product objProductInfo = new Product();

            objProductInfo.ID = Convert.ToInt32(dr["PK_ProductID"]);
            if (dr.Table.Columns.Contains("ParentCategoryID"))
            {
                objProductInfo.ParentCategoryID = Convert.ToInt32(dr["ParentCategoryID"]);
            }
            objProductInfo.CategoryID = Convert.ToInt32(dr["FK_CategoryID"]);
            objProductInfo.URL = Convert.ToString(dr["URL"]);
            objProductInfo.BrandID = Convert.ToInt32(dr["FK_BrandID"]);
            objProductInfo.SKU = Convert.ToString(dr["SKU"]);
            objProductInfo.Quantity = Convert.ToString(dr["Quantity"]);
            objProductInfo.UOMID = Convert.ToInt32(dr["FK_UOMID"]);
            objProductInfo.AvgRating = Convert.ToInt32(dr["AvgRating"]);
            objProductInfo.MRP = Convert.ToDecimal(dr["MRP"]);
            objProductInfo.Price = Convert.ToDecimal(dr["Price"]);
            objProductInfo.Image_Big = Convert.ToString(dr["Image_Big"]);
            objProductInfo.Image = Convert.ToString(dr["Image"]);
            objProductInfo.IsActive = Convert.ToBoolean(dr["IsActive"]);
            objProductInfo.LastModifiedBy = Convert.ToInt32(dr["LastModifiedBy"]);
            objProductInfo.LastModifiedDate = Convert.ToDateTime(dr["LastModifiedDate"]);
            objProductInfo.IsPackagedItem = Convert.ToBoolean(dr["IsPackagedItem"]);


            return objProductInfo;
        }

    }
}